﻿using System;
using System.Collections.Generic;
using System.Text;
using BarCode.Module;
using BarCode.Common;
using BarCode.Bean;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace BarCode.AccessDao
{
    public class BarCodeMdbDao
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(BarCodeMdbDao));

        private AccessHelper barcodeMdb;
        public BarCodeMdbDao()
        {
            barcodeMdb = new AccessHelper(Consts.MDB_BARCODE);
        }

        public BarCodeMdbDao(string barCodeMdbName)
        {
            barcodeMdb = new AccessHelper(barCodeMdbName);
        }

        public void Release()
        {
            barcodeMdb.Release();
        }

        /// <summary>
        /// 获取基本二维码信息
        /// </summary>
        /// <returns></returns>
        public List<TmpBarCodeInfo> getBarCodeList()
        {
            List<TmpBarCodeInfo> retList = new List<TmpBarCodeInfo>();
            DataSet retSet = barcodeMdb.Query("select * from tmp_barcode");
            if (retSet != null && retSet.Tables.Count > 0)
            {
                DataTable dt = retSet.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow tmpRow in dt.Rows)
                    {
                        TmpBarCodeInfo tmpInfo = new TmpBarCodeInfo();
                        tmpInfo.barcode = Convert.ToString(tmpRow["barcode"]);
                        tmpInfo.optName = Convert.ToString(tmpRow["Opt_Name"]);
                        tmpInfo.optDate = Convert.ToDateTime(tmpRow["OptDate"]);
                        retList.Add(tmpInfo);
                    }
                }
            }
            return retList;
        }

        /// <summary>
        /// 插入二维码信息
        /// </summary>
        /// <param name="lstLicence"></param>
        /// <returns></returns>
        public int insertBarCode(TmpBarCodeInfo barCodeInfo)
        {
            string insBarCodeSql = "INSERT INTO [tmp_barcode] ([barcode], [Opt_Name], [Opt_Date]) VALUES (@barcodeID, @Opt_Name, @Opt_Date )";
            OleDbParameter[] sqlParams = new OleDbParameter[3];
            sqlParams[0] = new OleDbParameter("@barcode", barCodeInfo.barcode);
            sqlParams[1] = new OleDbParameter("@Operator_Name", barCodeInfo.optName);
            sqlParams[2] = new OleDbParameter("@OpDt", barCodeInfo.optDate.ToString());
            int insCnt = barcodeMdb.ExecuteSql(insBarCodeSql, sqlParams);
            LogUtil.Debug("插入本地二维码信息");
            return insCnt;
        }


        /// <summary>
        /// 删除二维码信息
        /// </summary>
        /// <returns></returns>
        public int deleteBaseBarCode()
        {
            string delSql = "delete from [base_barcode] ";
            return barcodeMdb.ExecuteSql(delSql, new OleDbParameter[]{});
        }


        /// <summary>
        /// 获取指定用户当日的工作量
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int getWorkAmount(string userName)
        {
            OleDbParameter[] sqlParams = new OleDbParameter[2];
            sqlParams[0] = new OleDbParameter("@Opt_Name", userName);
            sqlParams[1] = new OleDbParameter("@Opt_Date", DateTime.Now.ToString("yyyy-MM-dd"));
            string amountSql = "select count(1) from tmp_barcode where [Opt_Name]=@Opt_Name AND Format([Opt_Date],'yyyy-mm-dd') = @Opt_Date";
            DataSet retSet = barcodeMdb.Query(amountSql, sqlParams);
            if (retSet != null && retSet.Tables.Count > 0)
            {
                DataTable dt = retSet.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    return Convert.ToInt32(dt.Rows[0][0]);
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }

        

    }
}
